package gold.digger;

import gold.vo.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1721 {
    public long startExecuteTime = System.currentTimeMillis();


    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    class Solution {
        public ListNode swapNodes(ListNode head, int k) {
            // 模拟指针，用来遍历链表
            ListNode cur = head;
            // 用来定位正数第k个节点
            ListNode first = head;
            // 用来定位倒数第k个节点
            ListNode last = head;
            // 用于节点的计数，和节点值的交换
            int count = 1;
            while (cur.next != null) {
                // 找到正数第k个节点
                if (count < k) {
                    first = first.next;
                    // 找到倒数第k个节点
                } else {
                    last = last.next;
                }
                count++;
                cur = cur.next;
            }
            // 交换正数第k个节点和倒数第k个节点的值
            count = first.val;
            first.val = last.val;
            last.val = count;
            return head;
        }
    }


    public void run() {
        Solution solution = new Solution();
        List<Integer> list = new ArrayList<>();
        System.out.println(solution.toString());
    }

    public static void main(String[] args) throws Exception {
        LC1721 an = new LC1721();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
